package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name = "查询所有角色信息")
    public String findAll(String companyId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Role> pageInfo = roleService.findByPage(page, pageSize, getCompanyId());
        request.setAttribute("page", pageInfo);
        return "/system/role/role-list";
    }

    @RequestMapping(value = "toAdd",name = "跳转到新增角色页面")
    public String add() {
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList", roleList);
        return "/system/role/role-add";
    }

    @RequestMapping(value = "/toUpdate",name = "跳转到修改角色信息")
    public String toUpdate(String id) {
        Role role = roleService.findById(id);
        request.setAttribute("role", role);
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList", roleList);
        return "/system/role/role-update";
    }

    @RequestMapping(value = "edit",name = "保存角色信息")
    public String save(Role role) {
        role.setCompanyId(getCompanyId());
        role.setCompanyName(getCompanyName());
        if (StringUtils.isEmpty(role.getId())) {
            String id = UUID.randomUUID().toString();
            role.setId(id);
            roleService.save(role);
        } else {
            roleService.updateById(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "delete",name = "删除部门信息")
    public String deleteById(String id) {
        roleService.deleteById(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "roleModule", name = "跳转到角色权限管理页面")
    public String roleModule(String roleid) {
        Role role = roleService.findById(roleid);
        request.setAttribute("role", role);
        return "system/role/role-module";
    }

    @RequestMapping(value = "getZtreeNodes",name = "服务器返回角色权限列表")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleid) {
        // 查询该角色具有的权限信息以便数据回显
        List<String> moduleIds = moduleService.findRoleModuleByRoleid(roleid);
        // 查询所有菜单信息
        List<Module> moduleList = moduleService.findAll();
        List<Map> list = new ArrayList<>();
        for (Module module : moduleList) {
            Map<String, String> map = new HashMap<>();
            map.put("id", module.getId());
            map.put("name", module.getName());
            map.put("pId", module.getParentId());
            // 判断当前module的id是否在该角色的中间表中，是则选择checked
            if (moduleIds.contains(module.getId())){
                map.put("checked","true");
            }
            list.add(map);
        }
        return list;
    }

    @RequestMapping(value = "updateRoleModule", name = "更新角色权限")
    public String updateRoleModule(String roleid, String moduleIds) {
        moduleService.updateRoleModule(roleid, moduleIds);
        return "redirect:/system/role/list.do";
    }
}
